<template>
  <div class="searchres">
    <van-nav-bar title="搜索结果" left-arrow @click-left="onClickLeft" />
    <div class="none icon-tuiguang1 iconfont" v-if="!goodsList.length">
      抱歉，暂未搜索到该商品~~
    </div>
    <div class="searchRes" v-else>
      <van-list
        v-model="loading"
        :finished="finished"
        finished-text="已经到底了，地主家已经没有余粮了~~"
        @load="onLoad"
      >
        <div class="pro_list">
          <div class="pro_item" v-for="(item, index) in goodsList" :key="index">
            <div class="img">
              <van-image class="pro_item_img" :src="item.photo">
                <template v-slot:loading>
                  <van-loading type="spinner" size="20" />
                </template>
              </van-image>
            </div>
            <span class="ellipsis-2">{{ item.title }}</span>
            <div class="bot">
              <span class="price">￥ {{ item.price }}</span>
              <p class="icon-gouwugouwuchedinggou iconfont"></p>
            </div>
          </div>
        </div>
      </van-list>
    </div>
  </div>
</template>

<script>
import { reqSearchApi } from "@/services";
export default {
  name: "searchres",
  data() {
    return {
      page: 1,
      size: 10,
      keywords: "",
      goodsList: [],
      loading: false,
      finished: false,
    };
  },
  created() {
    this.keywords = this.$route.query.keywords;
    this.onLoad();
  },
  methods: {
    onClickLeft() {
      this.$router.back();
    },
    async onLoad() {
      // 异步更新数据
      // setTimeout 仅做示例，真实场景中一般为 ajax 请求
      const { page, size, keywords } = this;
      const res = await reqSearchApi(page, size, keywords);
      if (res.code === 200) {
        // console.log(res.data.list);
        this.goodsList.push(...res.data);
        // 加载状态结束

        if (page >= 5) {
          // 加载状态结束
          this.finished = true;
          return;
        }
        this.page++;
      }
      this.loading = false;
      // console.log(res);
    },
  },
};
</script>

<style lang="less" spaced>
.van-nav-bar {
  background-color: #08dbdb;
  .van-icon {
    color: #fff;
  }
  .van-nav-bar__title {
    color: #fff;
  }
}
.searchres {
  .none {
    text-align: center;
    margin-top: 100px;
    color: #666;
    background-color: #fafafa;
  }
  .pro_list {
    display: flex;
    flex-wrap: wrap;
    padding: 0 40px;
    justify-content: space-between;
    .pro_item {
      width: 320px;
      height: 406px;
      margin-top: 20px;

      background-color: #fff;
      .img {
        width: 100%;
        height: 310px;
        // border: 1px solid red;
        .pro_item_img {
          width: 100%;
          height: 100%;
        }
      }
      span {
        font-size: 18px;
        line-height: 25px;
        height: 50px;
        margin-top: 10px;
      }
      .bot {
        display: flex;
        justify-content: space-between;
        span {
          color: red;
          font-size: 18px;
          font-weight: bold;
        }
        p {
          margin-right: 15px;
          font-size: 30px;
          color: #08dbdb;
          margin-top: 5px;
        }
      }
    }
  }
}
</style>
